package brand

import (
	"context"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"hosptial-record/api/base"
	"hosptial-record/forms"
	"hosptial-record/global"
	"hosptial-record/global/response"
	"hosptial-record/proto"
	"net/http"
)

// Create 创建品牌
func Create(ctx *gin.Context) {
	var form forms.BrandForm
	if err := ctx.ShouldBind(&form); err != nil {
		base.HandleValidatorError(ctx, err)
		return
	}
	params := proto.BrandRequest{
		Name: form.Name,
		Logo: form.Logo,
	}
	if res, err := global.GoodsSrvClient.CreateBrand(context.Background(), &params); err != nil {
		base.HandleGrpcErrorToHttp(err, ctx)
	} else {
		ctx.JSON(http.StatusOK, response.BaseResponse{
			Msg:  "创建成功",
			Data: res,
		})
	}
}

// Update 更新品牌
func Update(ctx *gin.Context) {
	var form forms.UpdateBrandForm
	if err := ctx.ShouldBind(&form); err != nil {
		base.HandleValidatorError(ctx, err)
		return
	}
	params := proto.BrandRequest{
		Id:   form.ID,
		Name: form.Name,
		Logo: form.Logo,
	}
	if _, err := global.GoodsSrvClient.UpdateBrand(context.Background(), &params); err != nil {
		base.HandleGrpcErrorToHttp(err, ctx)
	} else {
		ctx.JSON(http.StatusOK, response.BaseResponse{
			Msg: "更新成功",
		})
	}
}

// List 品牌列表
func List(ctx *gin.Context) {
	var form forms.BrandFilterForm
	if err := ctx.ShouldBindQuery(&form); err != nil {
		base.HandleValidatorError(ctx, err)
		return
	}
	params := proto.BrandFilterRequest{
		Pages:       form.Page,
		PagePerNums: form.PageSize,
	}
	if res, err := global.GoodsSrvClient.BrandList(context.Background(), &params); err != nil {
		base.HandleGrpcErrorToHttp(err, ctx)
	} else {
		ctx.JSON(http.StatusOK, response.BaseResponse{
			Msg:  "获取成功",
			Data: res,
		})
	}
}

// Delete 删除品牌
func Delete(ctx *gin.Context) {
	id, err := base.GetID2Param(ctx)
	if err != nil {
		zap.S().Errorf("ID不合法")
		ctx.JSON(http.StatusForbidden, "ID不合法")
		return
	}
	if _, err := global.GoodsSrvClient.DeleteBrand(context.Background(), &proto.BrandRequest{Id: id}); err != nil {
		base.HandleGrpcErrorToHttp(err, ctx)
	} else {
		ctx.JSON(http.StatusOK, response.BaseResponse{
			Msg: "删除成功",
		})
	}
}

// GetCategoryBrandList 通过分类id查询品牌
func GetCategoryBrandList(ctx *gin.Context) {
	id, err := base.GetID2Param(ctx)
	if err != nil {
		ctx.JSON(http.StatusForbidden, response.BaseResponse{
			Msg: "ID不合法",
		})
		return
	}
	params := proto.CategoryInfoRequest{
		Id: id,
	}
	if res, err := global.GoodsSrvClient.GetCategoryBrandList(context.Background(), &params); err != nil {
		base.HandleGrpcErrorToHttp(err, ctx)
	} else {
		ctx.JSON(http.StatusOK, response.BaseResponse{
			Msg:  "获取成功",
			Data: res,
		})
	}
}

// CreateCategoryBrand 创建品牌分类关联
func CreateCategoryBrand(ctx *gin.Context) {
	var form forms.BindCategoryForm
	if err := ctx.ShouldBind(&form); err != nil {
		base.HandleValidatorError(ctx, err)
		return
	}
	params := proto.CategoryBrandRequest{
		CategoryId: form.CategoryId,
		BrandId:    form.BrandId,
	}
	if res, err := global.GoodsSrvClient.CreateCategoryBrand(context.Background(), &params); err != nil {
		base.HandleGrpcErrorToHttp(err, ctx)
	} else {
		ctx.JSON(http.StatusOK, response.BaseResponse{
			Msg:  "创建成功",
			Data: res,
		})
	}
}

// DeleteCategoryBrand 删除品牌关联
func DeleteCategoryBrand(ctx *gin.Context) {
	id, err := base.GetID2Param(ctx)
	if err != nil {
		ctx.JSON(http.StatusForbidden, response.BaseResponse{
			Msg: "ID不合法",
		})
		return
	}
	if _, err := global.GoodsSrvClient.DeleteCategoryBrand(context.Background(), &proto.CategoryBrandRequest{Id: id}); err != nil {
		base.HandleGrpcErrorToHttp(err, ctx)
	} else {
		ctx.JSON(http.StatusOK, response.BaseResponse{
			Msg: "删除成功",
		})
	}
}
